#!/usr/bin/python
# -*- coding:utf-8 -*-
# name:typhoon，根据台风的年份与编号获取台风轨迹点
import requests
import json
import time
import xlwt

# 台风轨迹点
start_time = []  # 台风到达时间
lon = []  # 台风到达地经度
lat = []  # 台风到达地纬度
central_pressure = []  # 台风中心气压
wind = []  # 台风风速风力（米/秒）
direction = []  # 未来移向
feature_speed = []  # 未来移速
number_new = []  # 台风编号
name = []  # 台风名字
# 台风登陆时的轨迹点
start_time_1 = []  # 台风到达时间
lon_1 = []  # 台风到达地经度
lat_1 = []  # 台风到达地纬度
central_pressure_1 = []  # 台风中心气压
wind_1 = []  # 台风风速风力（米/秒）
direction_1 = []  # 未来移向
feature_speed_1 = []  # 未来移速
denglu = []  # 台风登陆描述
number_newDL = []  # 台风编号
name_newDL = []  # 台风名

# year = input("输入台风的年份") # 输入台风的年份
Year = [2020, 2021]
# number = input("输入台风的编号") # 输入台风的编号
Number = []
Number20 = [2023, 2022, 2021, 2020, 2019, 2018, 2017, 2016, 2015, 2014, 2013, 2012,
            2011, 2010, 2009, 2008, 2007, 2006, 2005, 2004, 2003, 2002, 2001]  # ,1800018,1800019
Number21 = [2112, 2110, 2111, 2109, 2108,
            2107, 2106, 2105, 2104, 2103, 2102, 2101]


for index in range(len(Year)):
    year = str(Year[index])
    if year == "2020":
        Number = Number20
    else:
        Number = Number21
    for index in range(len(Number)):
        number = str(Number[index])

    # 含时间戳的url，请求头
        url = "http://d1.weather.com.cn/typhoon/typhoon_data/"+year+"/"+number + \
            ".json?callback=getData&_="+str(int(round((time.time()*1000))))
        headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36 Edg/93.0.961.38",
            "Referer": "http://typhoon.weather.com.cn/gis/typhoon_p.shtml",
        }
        r = requests.get(url, headers=headers)
        print(r.text)
        a = json.loads(r.text[8:-1])  # 解析json文件
        print(a["typhoon"][8])
        print(a["typhoon"][2])
        print(len(a["typhoon"][8]))

        # print(a["typhoon"][8][21])
        for i in range(len(a["typhoon"][8])):
            #b = a["typhoon"][8][i]
            # print(b[1],b[4],b[5],b[6],b[7],b[8],b[9])
            start_time.append(a["typhoon"][8][i][1])
            lon.append(a["typhoon"][8][i][4])
            lat.append(a["typhoon"][8][i][5])
            central_pressure.append(a["typhoon"][8][i][6])
            wind.append(a["typhoon"][8][i][7])
            direction.append(a["typhoon"][8][i][8])
            feature_speed.append(a["typhoon"][8][i][9])
            number_new.append(number)
            name.append(a["typhoon"][2])
            if len(a["typhoon"][8][i]) > 13:
                start_time_1.append(a["typhoon"][8][i][1])
                lon_1.append(a["typhoon"][8][i][4])
                lat_1.append(a["typhoon"][8][i][5])
                central_pressure_1.append(a["typhoon"][8][i][6])
                wind_1.append(a["typhoon"][8][i][7])
                direction_1.append(a["typhoon"][8][i][8])
                feature_speed_1.append(a["typhoon"][8][i][9])
                denglu.append(a["typhoon"][8][i][13])
                number_newDL.append(number)
                name_newDL.append(a["typhoon"][2])

        f = xlwt.Workbook()  # 创建工作簿
        # 创建sheet,存放台风轨迹点（包括台风登陆时的轨迹点）
        sheet1 = f.add_sheet(u'sheet1', cell_overwrite_ok=True)
        # 创建sheet，存放台风登陆时的轨迹点
        sheet2 = f.add_sheet(u'sheet2', cell_overwrite_ok=True)
        j = 0
        q = 0
        for start_time1, lon1, lat1, central_pressure1, wind1, direction1, feature_speed1, number_new1, name1\
                in zip(start_time, lon, lat, central_pressure, wind, direction, feature_speed, number_new, name):
            sheet1.write(j, 0, number_new1)
            sheet1.write(j, 1, name1)
            sheet1.write(j, 2, start_time1)
            sheet1.write(j, 3, lon1)
            sheet1.write(j, 4, lat1)
            sheet1.write(j, 5, central_pressure1)
            sheet1.write(j, 6, wind1)
            sheet1.write(j, 7, direction1)
            sheet1.write(j, 8, feature_speed1)
            j = j+1
        for start_time1, lon1, lat1, central_pressure1, wind1, direction1, feature_speed1, denglu1, number_newDL1, name_newDL1\
                in zip(start_time_1, lon_1, lat_1, central_pressure_1, wind_1, direction_1, feature_speed_1, denglu, number_newDL, name_newDL):
            sheet2.write(q, 0, number_newDL1)
            sheet2.write(q, 1, name_newDL1)
            sheet2.write(q, 2, start_time1)
            sheet2.write(q, 3, lon1)
            sheet2.write(q, 4, lat1)
            sheet2.write(q, 5, central_pressure1)
            sheet2.write(q, 6, wind1)
            sheet2.write(q, 7, direction1)
            sheet2.write(q, 8, feature_speed1)
            sheet2.write(q, 9, denglu1)
            q = q+1
        f = f
        # f.save(number+".xls")#保存文件，文件名每一个台风更换不同的名称，我这里是以台风编号命名的。这个路径自己设定
f.save("TF总数据.xls")
